﻿
var cartCurrentlyOpen = null;

$(document).ready(function () {
    $(".mini-cart .summary")
           .attr("title", "Open")
           .click(function () {
               // kiem tra coi phai click vo login tab ko, neu co thi kiem dc class .login => check coi tab nao` dang click
               var isLogin = $(this).parents(".login").length > 0;
               var isRetail = !isLogin;
               // neu cart dang open => close cart do'
               if (cartCurrentlyOpen != null) {
                   var isOpeningOtherCart =
                       isLogin && cartCurrentlyOpen != "login" ||
                       isRetail && cartCurrentlyOpen != "retail";
                   closePanel(cartCurrentlyOpen, isOpeningOtherCart);
               }
               //
               if (!(cartCurrentlyOpen == "login" && isLogin) &&
                   !(cartCurrentlyOpen == "retail" && isRetail)) {
                   openPanel(isLogin ? "login" : "retail");
               }
           });

    // Assign the heading click to close the tab
    $(".mini-cart .heading a")
            .attr("title", "Close")
            .click(function () {
                closePanel($(this).parents(".login").length > 0 ? "login" : "retail", false);
                return false;
            });

});
function openPanel(cartToOpen) {

    var cart = $(".mini-cart");
    var cartStyleToLocate = "." + cartToOpen;
    var cartContent = cart.find(cartStyleToLocate);
    cartContent.removeClass("collapsed " + cartToOpen + "-collapsed").addClass("expanded " + cartToOpen + "-expanded");
    cart.removeClass("mini-cart-closed").addClass("mini-cart-open");
    cartContent.animate({ top: "0px" }, 300, function () {
        cartCurrentlyOpen = cartToOpen;
    });
}
function closePanel(cartToClose, isOpeningOtherCart) {

    var cart = $(".mini-cart");
    var cartStyleToLocate = "." + cartToClose;
    var cartContent = cart.find(cartStyleToLocate);

    cartContent.find(".body a").attr("tabindex", "-1");
    cart.find(cartStyleToLocate).animate({ top: "-267px" }, 300, function () {
        // callback function => goi sau khi animation completed
        if (isOpeningOtherCart !== true) {
            cart.removeClass("mini-cart-open")
                             .addClass("mini-cart-closed");
        }

        cartContent.removeClass("expanded " + cartToClose + "-expanded").addClass("collapsed " + cartToClose + "-collapsed");
        cartCurrentlyOpen = null;  // set de chi open tab khi tab khac ko open
    });
}
function addCart(productId, quantity, unitPrice, salePrice) {
    var webMethod = 'Webservices/Order.asmx/addCart';
    var parameters = "{'ProductId':'" + productId + "','Quantity':'" + quantity + "', 'ProductPrice':'" + unitPrice + "', 'SalePrice':'" + salePrice + "'}";
    $.ajax({
        type: "POST",
        url: webMethod,
        data: parameters,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        async: true,
        cache: false,
        success: function (msg) {
            //alert('Bạn đã đặt hàng thành công');
            window.location = "shoppingcart.aspx";
            //window.location.reload(true);
            /*
            $('body,html').animate({
            scrollTop: 0
            }, 900);

            $("div.mini-cart").removeClass("mini-cart-closed").addClass("mini-cart-open");
            $("div.retail").removeClass("collapsed retail-collapsed").addClass("expanded retail-expanded").css('top', '0px');
            */
        },
        error: function (e) {
            alert('Đã có lỗi');
        }
    });

    return true;
}